package com.fss.util;

import java.util.Map;  
  
import com.opensymphony.xwork2.Action;  
import com.opensymphony.xwork2.ActionContext;  
import com.opensymphony.xwork2.ActionInvocation;  
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
import com.fss.cmsContent.action.CmsContentAction;
import com.fss.cmsContent.service.CmsContentService;
import com.fss.common.action.CommonAction; 
import com.fss.util.Constants;  
import com.fssDataModels.TbCmsContent;
import com.fssDataModels.TbUser;
  
@SuppressWarnings("serial")
public class SessionIterceptor extends AbstractInterceptor {  
	private CmsContentService cmsContentService;


	@Override  
    public String intercept(ActionInvocation actionInvocation) throws Exception {  
        ActionContext ctx = ActionContext.getContext();  
        Map session = ctx.getSession();  
        Action action = (Action) actionInvocation.getAction();
        TbCmsContent  Cms_tmp = (TbCmsContent) session.get(Constants.BTN_1);
        TbUser User_temp = (TbUser)session.get(Constants.USER_LOGIN);
        
        session.remove(Action.ERROR);//for user_view page;
        if (Cms_tmp == null) {
        	System.out.println("intercept");
        	this.cmsContentService.getCMSContent();
        }
        
        if (action instanceof CmsContentAction) {
        	String admins[]={"fss_admin1","fss_admin2","fss_admin3"};
        	if(User_temp == null){
        		return Action.ERROR;
        	}
        	int i=0;
        	while(i<admins.length ){
                if (User_temp.getUserId().equals(admins[i])){
                	return actionInvocation.invoke();
                }
                ++i;
        	}
        	return Action.ERROR;
        }
        
        return actionInvocation.invoke();
    }


	public CmsContentService getCmsContentService() {
		return cmsContentService;
	}


	public void setCmsContentService(CmsContentService cmsContentService) {
		this.cmsContentService = cmsContentService;
	}
	
	
  
}  